let request_url = "";
const mockOpenid = "";
// const env = "dev";
const env = "prd";
const tabbarList = [
	"/pages/order",
	"/pages/userCenter"
]
if (env === "dev") {
	//开发环境
	request_url = "http://192.168.1.25:8088"
} else {
	request_url = "https://fx.fangxi666.com/prod-api"
	
}

export const GET = (url, data) => {
	return new Promise ((resolve, reject) => {
		uni.showLoading({
			mask: true
		})
		if (mockOpenid != "") {
			data.openId = mockOpenid
			uni.showToast({
				icon: 'none',
				title: '当前使用mockOpenid中'
			})
		}
		uni.request({
			url: request_url + url,
			method: 'GET',
			header: {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token')? `Bearer ${uni.getStorageSync('token')}` : ''
			},
			data: data,
			success: res => {
				uni.hideLoading()
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					// uni.showToast({
					// 	icon: 'none',
					// 	title: '登录态已失效',
					// 	duration: 2000
					// })
					if (tabbarList.includes(getCurrentPages()[0].$page.fullPath)) {
						// uni.reLaunch({
						// 	url: '/subPages/home/login'
						// })
					}
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: 'none',
						title: res.msg,
						duration: 2000
					})
				}
			}
		})
	})
}

export const POST = (url, data) => {
	return new Promise ((resolve, reject) => {
		uni.showLoading({
			mask: true
		})
		if (mockOpenid != "") {
			data.openId = mockOpenid
			uni.showToast({
				icon: 'none',
				title: '当前使用mockOpenid中'
			})
		}
		uni.request({
			url: request_url + url,
			method: 'POST',
			header: {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token')? `Bearer ${uni.getStorageSync('token')}` : ''
			},
			data: data,
			success: res => {
				uni.hideLoading()
				console.log(res)
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					// uni.showToast({
					// 	icon: 'none',
					// 	title: '登录态已失效',
					// 	duration: 2000
					// })
					if (tabbarList.includes(getCurrentPages()[0].$page.fullPath)) {
						uni.reLaunch({
							// url: '/subPages/home/login'
						})
					}
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: 'none',
						title: res.data.msg,
						duration: 2000
					})
				}
			}
		})
	})
}

export const PUT = (url, data) => {
	return new Promise ((resolve, reject) => {
		uni.showLoading({
			mask: true
		})
		if (mockOpenid != "") {
			data.openId = mockOpenid
			uni.showToast({
				icon: 'none',
				title: '当前使用mockOpenid中'
			})
		}
		uni.request({
			url: request_url + url,
			method: 'PUT',
			header: {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token')? `Bearer ${uni.getStorageSync('token')}` : ''
			},
			data: data,
			success: res => {
				uni.hideLoading()
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					// uni.showToast({
					// 	icon: 'none',
					// 	title: '登录态已失效',
					// 	duration: 2000
					// })
					if (tabbarList.includes(getCurrentPages()[0].$page.fullPath)) {
						// uni.reLaunch({
						// 	url: '/subPages/home/login'
						// })
					}
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: 'none',
						title: res.msg,
						duration: 2000
					})
				}
			}
		})
	})
}
export const DELETE = (url, data) => {
	return new Promise ((resolve, reject) => {
		uni.showLoading({
			mask: true
		})
		uni.request({
			url: request_url + url,
			method: 'DELETE',
			header: {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token')? `Bearer ${uni.getStorageSync('token')}` : ''
			},
			data: data,
			success: res => {
				console.log('返回response',res)
				uni.hideLoading()
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					// uni.showToast({
					// 	icon: 'none',
					// 	title: '登录态已失效',
					// 	duration: 2000
					// })
					if (tabbarList.includes(getCurrentPages()[0].$page.fullPath)) {
						// uni.reLaunch({
						// 	url: '/subPages/home/login'
						// })
					}
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: 'none',
						title: res.msg,
						duration: 2000
					})
				}
			}
		})
	})
}